/*
 *  Items.cpp
 *  SquirrelAdven
 *
 *  Created by James Weaver on 3/15/11.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 *
 */

#include "Items.h"
#include "OBJtoTriMesh.h"
#include <iostream>

// constructor
Items::Items() {
	itemID = -1;
	itemMesh = NULL;
}

// destructor
Items::~Items() {
}

void Items::computeBoundingBox() {
	float min_x, max_x, min_y, max_y, min_z, max_z;
	int i;
	int numVertices = itemMesh->GetNumVertices();
	
	// get the first vertice
	min_x = max_x = itemMesh->GetPosition(0)[0];
	min_y = max_y = itemMesh->GetPosition(0)[1];
	min_z = max_z = itemMesh->GetPosition(0)[2];
	
	for (i = 1; i < numVertices; i++) {
		Float3 v = itemMesh->GetPosition(i);
		
		// min x
		if (v[0] < min_x) {
			min_x = v[0];
		}
		
		// max x
		if (v[0] > max_x) {
			max_x = v[0];
		}
		
		// min y
		if (v[1] < min_y) {
			min_y = v[1];
		}
		
		// max y
		if (v[1] > max_y) {
			max_y = v[1];
		}
		
		// min z
		if (v[2] < min_z) {
			min_z = v[2];
		}
		
		// max z
		if (v[2] > max_z) {
			max_z = v[2];
		}
	}
	
	// compute length
	len_x = max_x - min_x;
	len_y = max_y - min_y;
	len_z = max_z - min_z;
	
	// compute center point
	//centerPoint[0] = (min_x + max_x) / 2;
	//centerPoint[1] = (min_y + max_y) / 2;
	//centerPoint[2] = (min_z + max_z) / 2;
	
	centerPoint[0] = 0;
	centerPoint[1] = 0;
	centerPoint[2] = 0;
	
	float half_len_x = len_x / 2;
	float half_len_z = len_z / 2;
	radius = half_len_x > half_len_z ? half_len_x :half_len_z;
}

// get TriMesh of the Item
TriMesh * Items::getItemMesh() {
	return itemMesh;
}

void Items::setItemID(int ID) {
	itemID = ID;
}
void Items::setItemTitle(std::string title) {
	itemTitle = title;
}

void Items::setItemType(int type) {
	itemType = type;
}

void Items::setObjFilename(std::string objFilename) {
	this->objFilename = objFilename;
}

void Items::setWmtfFilename(std::string wmtfFilename) {
	this->wmtfFilename = wmtfFilename;
	OBJtoTriMesh objToTriMesh;
	itemMesh = objToTriMesh.CreateMesh("./", objFilename, wmtfFilename);
	computeBoundingBox();
}

void Items::setTranslate(float x, float y, float z) {
	t_x = x;
	t_y = y;
	t_z = z;
}

int Items::getItemID() {
	return itemID;
}

std::string Items::getItemTitle() {
	return itemTitle;
}


int Items::getItemType() {
	return itemType;
}

float Items::getLengthX() {
	return len_x;
}

float Items::getLengthY() {
	return len_y;
}

float Items::getLengthZ() {
	return len_z;
}

float Items::getTranslateX() {
	return t_x;
}

float Items::getTranslateY() {
	return t_y;
}

float Items::getTranslateZ() {
	return t_z;
}

float Items::getRadius() {
	return radius;
}

Float3 Items::getCenterPoint() {
	return centerPoint;
}

void Items::modifyRadius(APoint scale_p) {
	float new_len_x = len_x * scale_p[0];
	float new_len_z = len_z * scale_p[2];
	
	float half_len_x = new_len_x / 2;
	float half_len_z = new_len_z / 2;
	radius = half_len_x > half_len_z ? half_len_x :half_len_z;
}

void Items::modifyCenterPoint(APoint translate_p) {
	centerPoint[0] += translate_p[0];
	centerPoint[1] += translate_p[1];
	centerPoint[2] += translate_p[2];
}

void Items::printData() {
	std::cout << "ID: " << itemID << "\n";
	std::cout << "Title: " << itemTitle << "\n";
	std::cout << "Type: " << itemType << "\n";
	std::cout << "ObjFilename: " << objFilename << "\n";
	std::cout << "Translation: " << t_x << ", " << t_y << ", " << t_z << "\n";
}